package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import net.wanho.dto.AddRoleDTO;
import net.wanho.dto.RoleAuthDTO;
import net.wanho.dto.RoleDTO;
import net.wanho.mapper.RoleAuthMapper;
import net.wanho.po.Role;
import net.wanho.po.RoleAuth;
import net.wanho.po.UserInfo;
import net.wanho.service.RoleAuthService;
import net.wanho.service.RoleService;
import net.wanho.vo.AjaxResult;
import net.wanho.vo.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/role")
public class RoleController {

    private final RoleService roleService;
    private final RoleAuthMapper roleAuthMapper;
    private final RoleAuthService roleAuthService;

    @GetMapping("/role-list")
    public AjaxResult roleList(){
        // 获取所有角色
        return AjaxResult.success("success",roleService.list());
    }

    @GetMapping("/role-page-list")
    public AjaxResult rolePageList(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "5") Integer pageSize,
                                   RoleDTO roleDTO){
        PageInfo<Role> pageInfo = roleService.rolePageList(pageNum,pageSize,roleDTO);
        return AjaxResult.success("success",pageInfo);
    }

    @PostMapping("/role-add")
    public AjaxResult roleAdd(@RequestBody AddRoleDTO addRoleDTO){
        roleService.addRole(addRoleDTO);
        return AjaxResult.success("角色添加成功");
    }

    @PutMapping("/role-state-update")
    public AjaxResult roleStateUpdate(@RequestBody Role role){
        roleService.updateState (role);
        return AjaxResult.success("角色状态修改成功");
    }

    @GetMapping("/{id}")
    public AjaxResult getRoleById(@PathVariable Integer id){
        Role role = roleService.selectById(id);
        return AjaxResult.success("success",role);
    }

    @PutMapping("/role-update")
    public AjaxResult roleUpdate(@RequestBody Role role){
        roleService.modifyById(role);
        return AjaxResult.success("角色修改成功");
    }

    @DeleteMapping("/role-delete/{id}")
    public AjaxResult roleDelete(@PathVariable Integer id){
        roleService.removeById(id);
        roleAuthService.removeByRoleId(id);
        return AjaxResult.success("角色删除成功");
    }

    @GetMapping("/role-auth")
    public AjaxResult roleAuth(@RequestParam Integer roleId){
        List<Integer> authIds = roleAuthMapper.selectByRoleId(roleId);
        return AjaxResult.success("success",authIds);
    }

    @PutMapping("/auth-grant")
    public AjaxResult authGrant(@RequestBody RoleAuthDTO roleAuthDTO){
        roleService.updateRoleAuth(roleAuthDTO);
        return AjaxResult.success("分配权限成功");
    }

    @SneakyThrows
    @GetMapping("/exportTable")
    public AjaxResult exportTable(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "5") int pageSize,
                                  RoleDTO roleDTO){
        PageInfo<Role> pageInfo = roleService.rolePageList(pageNum,pageSize,roleDTO);
        // 返回成功消息
        return AjaxResult.success("success",pageInfo.getResultList());

    }
}
